/*
 *Create By：Z.B
 *@DATE 2019/3/18 10:00
 *@EMAIL: 2545375@qq.com
 */
'use strict';

module.exports = (option, app) => {
  return async function(ctx, next){
    try{
      await next();
    }catch(err){
      app.emit('error',err,this);
      const status = err.status || 500;
      const error = status === 500 && app.config.env === 'prod' ?
          'Internal Server Error': err.message;
      // 从 error 对象上读出各个属性，设置到响应中
      ctx.body = {
        code: status,
        error: error,
      }
      if (status === 422){
        ctx.body.detail = err.errors
      }
      ctx.status = 200
    }
  }
};